import { createRouter, createWebHistory } from 'vue-router'
import { useMetaStore } from '@/stores'
import { userTokenStore } from '@/stores'

// 路由name属性，首字母大写，和菜单index对应
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/index',
      name: 'Layout',
      component: () => import('@/views'),
      meta: {
        title: '云梦智学-主页',
        name: '主页',
      },
      children: [
        {
          path: '',
          name: 'Dashboard',
          component: () => import('@/views/Dashboard'),
          meta: {
            title: '云梦智学-主页',
            name: '数据总览',
          },
        },
        {
          path: '/users',
          name: 'Users',
          redirect: '/users/admins',
          meta: {
            title: '云梦智学-用户管理',
            name: '用户管理',
          },
          children: [
            {
              path: '/users/admins',
              name: 'Admins',
              component: () => import('@/views/users/AdminManagement'),
              meta: {
                title: '云梦智学-管理员信息',
                name: '管理员信息',
              },
            },
            {
              path: '/users/students',
              name: 'Students',
              component: () => import('@/views/users/StudentManagement'),
              meta: {
                title: '云梦智学-学生信息',
                name: '学生信息',
              },
            },
            {
              path: '/users/teachers',
              name: 'Teachers',
              component: () => import('@/views/users/TeacherManagement'),
              meta: {
                title: '云梦智学-教师信息',
                name: '教师信息',
              },
            },
          ],
        },
        {
          path: '/questions',
          name: 'Questions',
          component: () => import('@/views/questions/TechCategory'),
          meta: {
            title: '云梦智学-技术方向',
            name: '技术方向',
          },
          },
          {
            path: '/questions/knowledge',
            name: 'KnowledgePoints',
            component: () => import('@/views/questions/Knowledge'),
            meta: {
              title: '云梦智学-知识点', 
              name: '知识点',
            },
          },
          {
            path: '/questions/knowledge/stages',
            name: 'Stage',
            component: () => import('@/views/questions/Stage'),
            meta: {
              title: '云梦智学-关卡', 
              name: '关卡',
            },
          },
          {
            path: '/questions/knowledge/stages/examination',
            name: 'Examination',
            component: () => import('@/views/questions/Examination'),
            meta: {
              title: '云梦智学-题库', 
              name: '题库',
            },
          },
        {
          path: '/knowledge',
          name: 'Knowledge',
          component: () => import('@/views/knowledges'),
          meta: {
            title: '云梦智学-知识图谱',
            name: '知识图谱',
          },
        },
         {
          path: '/knowledgeGraph',
          name: 'KnowledgeGraph',
          component: () => import('@/views/Knowledge'),
          meta: {
            title: '云梦智学-知识图谱',
            name: '知识图谱',
          },
        },
        {
          path: '/content',
          name: 'Content',
          redirect: '/content/videos',
          meta: {
            title: '云梦智学-内容管理',
            name: '内容管理',
          },
          children: [
            {
              path: '/content/videos',
              name: 'Videos',
              component: () => import('@/views/content/Videos'),
              meta: {
                title: '云梦智学-视频课程',
                name: '视频课程',
              },
            },
            {
              path: '/content/topics',
              name: 'Topics',
              component: () => import('@/views/content/Topics'),
              meta: {
                title: '云梦智学-话题社区',
                name: '话题社区',
              },
            },
            {
              path: '/content/pk',
              name: 'Pk',
              component: () => import('@/views/content/PK'),
              meta: {
                title: '云梦智学-PK竞赛',
                name: 'PK竞赛',
              },
            },
          ],
        },
        {
          path: '/system',
          name: 'System',
          redirect: '/system/roles',
          meta: {
            title: '云梦智学-系统管理',
            name: '系统管理',
          },
          children: [
            {
              path: '/system/roles',
              name: 'Roles',
              component: () => import('@/views/system/RoleManagement'),
              meta: {
                title: '云梦智学-角色管理',
                name: '角色管理',
              },
            },
            {
              path: '/system/permissions',
              name: 'Permissions',
              component: () => import('@/views/system/PermissionManagement'),
              meta: {
                title: '云梦智学-权限管理',
                name: '权限管理',
              },
            },
            {
              path: '/system/reports',
              name: 'Reports',
              component: () => import('@/views/system/Reports'),
              meta: {
                title: '云梦智学-数据报表',
                name: '数据报表',
              },
            },
            {
              path: '/system/settings',
              name: 'Settings',
              component: () => import('@/views/system/Settings'),
              meta: {
                title: '云梦智学-系统设置',
                name: '系统设置',
              },
            },
            {
              path: '/system/audit',
              name: 'Audit',
              component: () => import('@/views/system/Audit'),
              meta: {
                title: '云梦智学-操作日志',
                name: '操作日志',
              },
            },
          ],
        },
        {
          path: '/center',
          name: 'center',
          component: () => import('@/views/center'),
          meta: {
            title: '云梦智学-个人中心',
            name: '个人中心',
          },
        },
      ],
    },
    {
      path: '/',
      name: 'LoginIndex',
      component: () => import('@/views/login'),
      meta: {
        title: '云梦智学-登录',
        name: '登录',
      },
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/login/register'),
      meta: {
        title: '云梦智学-注册',
        name: '注册',
      },
    },
    {
      path: '/password',
      name: 'Password',
      component: () => import('@/views/login/password'),
      meta: {
        title: '云梦智学-找回密码',
        name: '找回密码',
      },
    },
    {
      path: '/test',
      name: 'Test',
      component: () => import('@/views/test'),
      meta: {
        title: '云梦智学-测试页面',
        name: '测试页面',
      },
    },
  ],
})

router.beforeEach((to, from, next) => {
  const tokenStore = userTokenStore()
  if (
    tokenStore.getToken() ||
    to.path === '/' ||
    to.path === '/register' ||
    to.path === '/password'
  ) {
    const metaStore = useMetaStore()
    const array = []
    for (const i in to.matched) {
      array.push({
        name: to.matched[i].meta.name,
        path: to.matched[i].path,
      })
    }
    metaStore.setMeta(array)
    document.title = to.meta.title
    next()
  } else {
    next('/')
  }
})
export default router
